# -*- coding: utf-8 -*-
# @Time    : 2020/10/22 13:59
# @Author  : Leo
# @FileName: user_model.py
# @Software: PyCharm


'''连接MongoDB'''

import os
from pymongo import MongoClient
from time import strftime, localtime
from bson.objectid import ObjectId


class UserModel(object):
    orderTable = None

    def __init__(self):
        self.client, self.orderTable = self.get_clientAndTable()

    def get_list_from_cursor(self, cursor):
        user_list = []
        for user in cursor:
            user.update({'user_id': str(user['_id'])})
            user.pop('_id')
            user_list.append(user)
        return user_list

    def get_clientAndTable(self):
        client = MongoClient(
            "mongodb://{0}:{1}@{2}/admin".format(
                "root",
                "eisoo.com",
                "10.2.184.160:27017,10.2.184.161:27017,10.2.184.162:27017"))
        db = client.userOrder
        orderTable = db.userOrderTable
        return client, orderTable

    def close_client(self, client):
        client.close()

    def get_allData(self):
        '''
        获取所有用户
        @return:
        '''
        # user_list = []
        # for test_user in self.orderTable.find():
        #     test_user.update({'user_id': str(test_user['_id'])})
        #     user_list.append(test_user)
        return self.get_list_from_cursor(self.orderTable.find())

    def get_dataByName(self, name):
        return self.get_list_from_cursor(self.orderTable.find({"name": name}))

    def get_dataByID(self, id):
        '''
        根据user_id获取用户
        @param id:
        @return:
        '''
        if len(id) < 24:
            return  []
        return self.get_list_from_cursor(
            self.orderTable.find({"_id": ObjectId(id)}))

    def insert_Data(
            self,
            name,
            pwd,
            isOrder=0,
            position="sh",
            orderType="Launch",
            orderTime=strftime(
                "%Y-%m-%d %H:%M:%S",
                localtime()),
            weChartID="",
            email="",
            isUse=0,
            order_status=0):
        values = [0, 1]
        if (isOrder not in values) and (isUse not in values) and (
                self.orderTable not in values):
            return False
        self.orderTable.insert_one({"name": name,
                                    "pwd": pwd,
                                    "isOrder": isOrder,
                                    "orderTime": orderTime,
                                    "position": position,
                                    "orderType": orderType,
                                    "weChartID": weChartID,
                                    "email": email,
                                    "isUse": isUse,
                                    "order_status": order_status})
        return True

    def update_orderDataByName(
            self,
            name,
            isOrder=0,
            orderTime=strftime(
                "%Y-%m-%d %H:%M:%S",
                localtime()),
            position="sh",
            orderType="Launch"):
        self.orderTable.update_one({"name": name},
                                   {"$set": {"isOrder": isOrder,
                                             "orderTime": orderTime,
                                             "position": position,
                                             "orderType": orderType}})

    def update_manageDataByName(self, name, isUse=0, order_status=0):
        self.orderTable.update_one(
            {"name": name}, {"$set": {"isUse": isUse, "order_status": order_status}})

    def update_userDataByName(self, name, weChartID="weChartID"):
        self.orderTable.update_one(
            {"name": name}, {"$set": {"weChartID": weChartID}})

#
# {'name': 'jack',
#     'pwd': '123456',
#     'isOrder': 0,
#     'orderTime': '2020-10-22 17:08:45',
#     'position': 'sh',
#     'orderType': 'Launch',
#     'weChartID': '1557596349',
#     'email': '',
#     'isUse': 0,
#     'order_status': 0,
#  'user_id': '5f914c1d1b3c13969990221d'}




a = UserModel().get_allData()
for i in a:
    print(i['name'], i['user_id'], 'isOrder:',i['isOrder'], 'wechatId:', i['weChartID'])
# b = 3;
# for i in a:
#     b = 1
# pass
